package com.example.springbootcaffeinedemo.common1;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/04/18  14:23
 * @Description: 测试Caffeine的最大权重maximumWeight
 */
public class CaffeineWeightTest {
    public static void main(String[] args) throws InterruptedException {
        Caffeine<String, Person> caffeine = Caffeine.newBuilder()
                .maximumWeight(30)
                // 设置权重值规则
                .weigher((String key, Person value) -> value.getAge());
        Cache<String, Person> cache = caffeine.build();

        cache.put("one",new Person(12,"noe"));
        cache.put("two",new Person(3,"two"));
        cache.put("three",new Person(18,"three"));

        TimeUnit.SECONDS.sleep(2);

        // 缓存容量
        System.out.println(cache.estimatedSize());
        System.out.println(cache.getIfPresent("one"));
        System.out.println(cache.getIfPresent("two"));
        System.out.println(cache.getIfPresent("three"));
    }
}
